﻿//==============================================================================
// File: ControlBehavior.cs
// Created: 2010-05-28
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace WpfRcp.Controls.Behaviors
{
    public class ControlBehavior : Behavior<Control>
    {
        #region DoubleClickCommand Attached Property

        public static readonly DependencyProperty DoubleClickCommandProperty =
            DependencyProperty.RegisterAttached("DoubleClickCommand", typeof(ICommand), typeof(ControlBehavior));

        public static ICommand GetDoubleClickCommand(Control control)
        {
            return (ICommand)control.GetValue(DoubleClickCommandProperty);
        }

        public static void SetDoubleClickCommand(Control control, ICommand value)
        {
            control.SetValue(DoubleClickCommandProperty, value);
        }

        #endregion

        #region LeftMouseClickCommand Attached Property

        public static readonly DependencyProperty LeftMouseClickCommandProperty =
            DependencyProperty.RegisterAttached("LeftMouseClickCommand", typeof(ICommand), typeof(ControlBehavior));

        public static ICommand GetLeftMouseClickCommand(Control control)
        {
            return (ICommand)control.GetValue(LeftMouseClickCommandProperty);
        }

        public static void SetLeftMouseClickCommand(Control control, ICommand value)
        {
            control.SetValue(LeftMouseClickCommandProperty, value);
        }

        #endregion

        #region RightMouseClickCommand Attached Property

        public static readonly DependencyProperty RightMouseClickCommandProperty =
            DependencyProperty.RegisterAttached("RightMouseClickCommand", typeof(ICommand), typeof(ControlBehavior));

        public static ICommand GetRightMouseClickCommand(Control control)
        {
            return (ICommand)control.GetValue(RightMouseClickCommandProperty);
        }

        public static void SetRightMouseClickCommand(Control control, ICommand value)
        {
            control.SetValue(RightMouseClickCommandProperty, value);
        }

        #endregion

        #region CommandParameter Attached Property

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(ControlBehavior), null);

        public static object GetCommandParameter(Control control)
        {
            return control.GetValue(CommandParameterProperty);
        }

        public static void SetCommandParameter(Control control, object value)
        {
            control.SetValue(CommandParameterProperty, value);
        }

        #endregion

        protected override void OnAttached()
        {
            AssociatedObject.MouseDoubleClick += MouseDoubleClickHandler;
            AssociatedObject.MouseDown += MouseDownHandler;
        }
        
        protected override void OnDetaching()
        {
            AssociatedObject.MouseDoubleClick -= MouseDoubleClickHandler;
            AssociatedObject.MouseDown -= MouseDownHandler;
        }

        private static void MouseDoubleClickHandler(object sender, MouseButtonEventArgs e)
        {
            var element = (Control)sender;
            var command = GetDoubleClickCommand(element);
            var commandParameter = GetCommandParameter(element);
            
            if (command != null)
            {
                command.Execute(commandParameter);   
            }
        }

        private static void MouseDownHandler(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 1)
            {
                var element = (Control)sender;
                var commandParameter = GetCommandParameter(element);
                ICommand command = null;

                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    command = GetLeftMouseClickCommand(element);
                }
                else if (e.RightButton == MouseButtonState.Pressed)
                {
                    command = GetRightMouseClickCommand(element);
                }
                
                if (command != null)
                {
                    command.Execute(commandParameter);
                }
            }
        }
    }
}